OPC Studio User's Guide and Reference
Examples - OPC Unified Architecture - Browse data nodes recursively
// This example shows how to obtain "data nodes" under the "Objects" node, recursively.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System;
using System.Diagnostics;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.AddressSpace;
using OpcLabs.EasyOpc.UA.AddressSpace.Standard;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UADocExamples._EasyUAClient
{
    partial class BrowseDataNodes
    {
        public static void Recursive()
        {
            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            // Instantiate the client object
            var client = new EasyUAClient();

            try
            {
                BrowseFromNode(client, endpointDescriptor, UAObjectIds.ObjectsFolder, level:0);
            }
            catch (UAException uaException)
            {
                Console.WriteLine($"*** Failure: {uaException.GetBaseException().Message}");
            }
        }

        private static void BrowseFromNode(
            EasyUAClient client,
            UAEndpointDescriptor endpointDescriptor,
            UANodeDescriptor parentNodeDescriptor,
            int level)
        {
            Debug.Assert(!(client is null));
            Debug.Assert(!(endpointDescriptor is null));
            Debug.Assert(!(parentNodeDescriptor is null));

            // Obtain all node elements under parentNodeDescriptor
            UANodeElementCollection nodeElementCollection = 
                client.BrowseDataNodes(endpointDescriptor, parentNodeDescriptor);
            // Remark: BrowseDataNodes(...) may throw UAException; we handle it in the calling method.

            foreach (UANodeElement nodeElement in nodeElementCollection)
            {
                Debug.Assert(!(nodeElement is null));

                Console.Write(new string(' ', level*2));    // indent
                Console.WriteLine(nodeElement);

                // Browse recursively into the node.
                // The UANodeElement has an implicit conversion to UANodeDescriptor.
                BrowseFromNode(client, endpointDescriptor, nodeElement, level + 1);

                // Note that the number of nodes you obtain through recursive browsing may be very large, or even infinite.
                // Production code should contain appropriate safeguards for these cases.
            }
        }


        // Example output:
        //
        //ServerStatus -> nsu=http://opcfoundation.org/UA/ ;i=2256 (Variable) 
        //  StartTime -> nsu=http://opcfoundation.org/UA/ ;i=2257 (Variable) 
        //  CurrentTime -> nsu=http://opcfoundation.org/UA/ ;i=2258 (Variable) 
        //  State -> nsu=http://opcfoundation.org/UA/ ;i=2259 (Variable) 
        //  BuildInfo -> nsu=http://opcfoundation.org/UA/ ;i=2260 (Variable) 
        //    ProductUri -> nsu=http://opcfoundation.org/UA/ ;i=2262 (Variable) 
        //    ManufacturerName -> nsu=http://opcfoundation.org/UA/ ;i=2263 (Variable) 
        //    ProductName -> nsu=http://opcfoundation.org/UA/ ;i=2261 (Variable) 
        //    SoftwareVersion -> nsu=http://opcfoundation.org/UA/ ;i=2264 (Variable) 
        //    BuildNumber -> nsu=http://opcfoundation.org/UA/ ;i=2265 (Variable) 
        //    BuildDate -> nsu=http://opcfoundation.org/UA/ ;i=2266 (Variable) 
        //  SecondsTillShutdown -> nsu=http://opcfoundation.org/UA/ ;i=2992 (Variable) 
        //  ShutdownReason -> nsu=http://opcfoundation.org/UA/ ;i=2993 (Variable) 
        //ServerCapabilities -> nsu=http://opcfoundation.org/UA/ ;i=2268 (Object) 
        //  OperationLimits -> nsu=http://opcfoundation.org/UA/ ;i=11704 (Object) 
        //    MaxNodesPerRead -> nsu=http://opcfoundation.org/UA/ ;i=11705 (Variable) 
        //    MaxNodesPerHistoryReadData -> nsu=http://opcfoundation.org/UA/ ;i=12165 (Variable) 
        //    MaxNodesPerHistoryReadEvents -> nsu=http://opcfoundation.org/UA/ ;i=12166 (Variable) 
        //    MaxNodesPerWrite -> nsu=http://opcfoundation.org/UA/ ;i=11707 (Variable) 
        //    MaxNodesPerHistoryUpdateData -> nsu=http://opcfoundation.org/UA/ ;i=12167 (Variable) 
        //...
    }
}
# This example shows how to obtain "data nodes" under the "Objects" node, recursively.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.AddressSpace import *
from OpcLabs.EasyOpc.UA.AddressSpace.Standard import *
from OpcLabs.EasyOpc.UA.OperationModel import *


def browseFromNode(client, endpointDescriptor, parentNodeDescriptor, level):
    assert client is not None
    assert endpointDescriptor is not None
    assert parentNodeDescriptor is not None

    # Obtain all node elements under parentNodeDescriptor
    nodeElementCollection = IEasyUAClientExtension.BrowseDataNodes(client, endpointDescriptor, parentNodeDescriptor)
    # Remark: BrowseDataNodes(...) may throw UAException; we handle it in the calling method.
    #
    for nodeElement in nodeElementCollection:
        assert nodeElement is not None

        print(' '*(level*2), end='')    # indent
        print(nodeElement)

        # Browse recursively into the node.
        # The UANodeElement has an implicit conversion to UANodeDescriptor.
        browseFromNode(client, endpointDescriptor, nodeElement.ToUANodeDescriptor(), level + 1)

        # Note that the number of nodes you obtain through recursive browsing may be very large, or even infinite.
        # Production code should contain appropriate safeguards for these cases.


endpointDescriptor = UAEndpointDescriptor('opc.tcp://opcua.demo-this.com:51210/UA/SampleServer')
# or 'http://opcua.demo-this.com:51211/UA/SampleServer' (currently not supported)
# or 'https://opcua.demo-this.com:51212/UA/SampleServer/'

# Instantiate the client object.
client = EasyUAClient()

try:
    browseFromNode(client, endpointDescriptor, UANodeDescriptor(UAObjectIds.ObjectsFolder), 0)
except UAException as uaException:
    print('*** Failure: ' + uaException.GetBaseException().Message)
    exit()

print()
print('Finished.')
' This example shows how to obtain "data nodes" under the "Objects" node, recursively.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.AddressSpace
Imports OpcLabs.EasyOpc.UA.AddressSpace.Standard
Imports OpcLabs.EasyOpc.UA.OperationModel

Namespace _EasyUAClient
    Partial Friend Class BrowseDataNodes
        Public Shared Sub Recursive()

            ' Define which server we will work with.
            Dim endpointDescriptor As UAEndpointDescriptor =
                    "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"
            ' or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            ' or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            ' Instantiate the client object
            Dim client = New EasyUAClient()
            Try
                BrowseFromNode(client, endpointDescriptor, UAObjectIds.ObjectsFolder, level:=0)
            Catch uaException As UAException
                Console.WriteLine($"*** Failure: {uaException.GetBaseException().Message}")
                Exit Sub
            End Try
        End Sub

        Private Shared Sub BrowseFromNode(client As EasyUAClient, endPointDescriptor As UAEndpointDescriptor, parentNodeDescriptor As UANodeDescriptor, level As Integer)
            Debug.Assert(client IsNot Nothing)
            Debug.Assert(endPointDescriptor IsNot Nothing)
            Debug.Assert(parentNodeDescriptor IsNot Nothing)

            ' Obtain all node elements under parentNodeDescriptor
            Dim nodeElementCollection As UANodeElementCollection =
                client.BrowseDataNodes(endPointDescriptor, parentNodeDescriptor)
            ' Remark: BrowseDataNodes(...) may throw UAException; we handle it in the calling method.

            For Each nodeElement As UANodeElement In nodeElementCollection
                Debug.Assert(nodeElement IsNot Nothing)

                Console.Write(New String(" "c, level * 2))
                Console.WriteLine(nodeElement)

                ' Browse recursively into the node.
                ' The UANodeElement has an implicit conversion to UANodeDescriptor.
                BrowseFromNode(client, endPointDescriptor, nodeElement, level + 1)

                ' Note that the number of nodes you obtain through recursive browsing may be very large, or even infinite.
                ' Production code should contain appropriate safeguards for these cases.
            Next nodeElement
        End Sub
    End Class
End Namespace

 

See Also

Examples - OPC Data Access